package com.jsoft.jsofted.sys.controller;

import com.diboot.core.service.BaseService;
import com.diboot.core.util.BeanUtils;
import com.jsoft.jsofted.framework.annotation.JwtToken;
import com.jsoft.jsofted.sys.entity.OrgCorpEntity;
import com.jsoft.jsofted.sys.entity.OrgEntity;
import com.jsoft.jsofted.sys.service.CorpService;
import com.jsoft.jsofted.sys.service.OrgService;
import com.jsoft.jsofted.framework.utils.ResponseModel;
import com.jsoft.jsofted.sys.vo.OrgCorpView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.LinkedList;

@RestController
@RequestMapping("/jsofted/corp")
public class CorpController {

    @Autowired
    private CorpService corpService;
    @Autowired
    private OrgService orgService;

    @JwtToken
    @RequestMapping("/add")
    public ResponseModel add(@RequestBody OrgCorpView entity) {
        Timestamp time = new Timestamp(System.currentTimeMillis());
        entity.setCreateTime(time);
        entity.setUpdateTime(time);
        entity.setSortValue(new BigDecimal(time.getTime()));

        // 保存机构数据
        OrgEntity orgEntity = new OrgEntity();
        BeanUtils.copyProperties(entity, orgEntity);
        boolean success = orgService.save(orgEntity);

        // 保存公司数据
        OrgCorpEntity corpEntity = new OrgCorpEntity();
        BeanUtils.copyProperties(entity, corpEntity);
        corpEntity.setId(orgEntity.getId());
        success = success && corpService.save(corpEntity);

        // 返回
        if (success) {
            return ResponseModel.makeSuccess(null, corpEntity);
        } else {
            return ResponseModel.makeError(null);
        }
    }


    @JwtToken
    @RequestMapping("/update/{id}")
    public ResponseModel update(
            @PathVariable Long id,
            @RequestBody OrgCorpView entity
    ) {
        entity.setId(id);

        // 保存机构数据
        OrgEntity orgEntity = new OrgEntity();
        BeanUtils.copyProperties(entity, orgEntity);
        boolean success = orgService.updateById(orgEntity);

        // 保存公司数据
        OrgCorpEntity corpEntity = new OrgCorpEntity();
        BeanUtils.copyProperties(entity, corpEntity);
        corpEntity.setId(orgEntity.getId());
        success = success && corpService.updateById(corpEntity);

        // 返回
        if (success) {
            return ResponseModel.makeSuccess(null, corpEntity);
        } else {
            return ResponseModel.makeError(null);
        }
    }

    @JwtToken
    @RequestMapping("/delete/{id}")
    public ResponseModel delete(
            @PathVariable(required = false) String id,
            @RequestBody(required = false) LinkedList<String> idList
    ) {
        boolean success = false;
        if (idList != null && idList.size() > 0) {
            success = orgService.removeByIds(idList);
            success = success && corpService.removeByIds(idList);
        } else if (id != null && id.trim().length() > 0) {
            success = orgService.removeById(id);
            success = success && corpService.removeById(id);
        }

        if (success) {
            return ResponseModel.makeSuccess(null, null);
        } else {
            return ResponseModel.makeError(null);
        }
    }

    @JwtToken
    @RequestMapping("/get/{id}")
    public ResponseModel get(@PathVariable String id) {
        BaseService diOrgService = (BaseService) orgService;
        BaseService diCorpService = (BaseService) corpService;

        OrgEntity orgEntity = (OrgEntity) diOrgService.getViewObject(id, OrgEntity.class);
        OrgCorpEntity corpEntity = (OrgCorpEntity) diCorpService.getViewObject(id, OrgCorpEntity.class);

        OrgCorpView corpBean = new OrgCorpView();
        BeanUtils.copyProperties(corpEntity, corpBean);
        BeanUtils.copyProperties(orgEntity, corpBean);

        if (corpEntity != null) {
            return ResponseModel.makeSuccess(null, corpBean);
        } else {
            return ResponseModel.makeError("数据不存在！");
        }
    }

}
